package org.dromara.business.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.BusinessComplaintBo;
import org.dromara.business.domain.vo.BusinessComplaintVo;
import org.dromara.business.domain.BusinessComplaint;
import org.dromara.business.mapper.BusinessComplaintMapper;
import org.dromara.business.service.BusinessComplaintService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 投诉Service业务层处理
 *
 * @author zlj
 * @date 2024-11-18
 */
@RequiredArgsConstructor
@Service
public class BusinessComplaintServiceImpl extends ServiceImpl<BusinessComplaintMapper,BusinessComplaint> implements BusinessComplaintService {

    private final BusinessComplaintMapper baseMapper;

    /**
     * 查询投诉
     */
    @Override
    public BusinessComplaintVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询投诉列表
     */
    @Override
    public TableDataInfo<BusinessComplaintVo> queryPageList(BusinessComplaintBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusinessComplaint> lqw = buildQueryWrapper(bo);
        Page<BusinessComplaintVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询投诉列表
     */
    @Override
    public List<BusinessComplaintVo> queryList(BusinessComplaintBo bo) {
        LambdaQueryWrapper<BusinessComplaint> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusinessComplaint> buildQueryWrapper(BusinessComplaintBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusinessComplaint> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getNr()), BusinessComplaint::getNr, bo.getNr());
        lqw.eq(bo.getUserId() != null, BusinessComplaint::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getType()), BusinessComplaint::getType, bo.getType());
        lqw.orderByDesc(BusinessComplaint::getCreateTime);
        return lqw;
    }

    /**
     * 新增投诉
     */
    @Override
    public Boolean insertByBo(BusinessComplaintBo bo) {
        BusinessComplaint add = MapstructUtils.convert(bo, BusinessComplaint.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改投诉
     */
    @Override
    public Boolean updateByBo(BusinessComplaintBo bo) {
        BusinessComplaint update = MapstructUtils.convert(bo, BusinessComplaint.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusinessComplaint entity){

    }

    /**
     * 批量删除投诉
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
